<?php


namespace App\Log;
use App\Models\Admin\GoodsDatum;
use Illuminate\Support\Arr;
use Auth;

class AdminDevLog extends AdminLog
{
    protected $pre = [];    //修改前
    protected $next = [];   //修改后
    protected $module;      //操作模块

    protected $mean = [     //对应字段
        'name' => '设备名称',
        'status'    => [1=>'上架',0=>'下架'],
        'use_inventory'   => [1=>'开启库存',0=>'关闭库存'],
        'pics' => '图片',
        'pic' => '图片',
        'specify' => [1=>'开启',0=>'关闭'],
        'spu' => 'spu编码',
        'detail' => '图文详情',
    ];

    protected $other = [    //数组对应字段
        'status' => '状态',
        'use_inventory' => '是否开启库存',
    ];

    public function __construct($pre,$next,$module)
    {
        $this->pre = $pre;
        $this->next = $next;
        $this->module = $module;
    }

    public function addLog()
    {
        $this->data = $this->combinedData();
        parent::addLog(); // TODO: Change the autogenerated stub
    }

    //组合数据
    protected function combinedData():array
    {
        $content = [];
        if (!empty( $this->next)){
            foreach ( $this->next as $key => $val){
                if (isset( $this->mean[$key])){
                    if (isset( $this->other[$key])){
                        $meanInfo = "'". $this->other[$key]."'";
                    }else{
                        $meanInfo = "'". $this->mean[$key]."'";
                    }
                    if (is_array( $this->mean[$key])){
                        if ($key == 'label_ids'){
                            $preStr = $nextStr = '';
                            $preArr = $nextArr = [];
                            if ($this->pre['label_ids']){
                                $preArr = explode(',', $this->pre['label_ids']);
                            }
                            if ($this->next['label_ids']){
                                $nextArr = explode(',', $this->next['label_ids']);
                            }
                            if (!empty($preArr)){
                                foreach ($preArr as $id){
                                    $preStr.=  $this->mean[$key][$id].' ';
                                }
                            }
                            if (!empty($nextArr)){
                                foreach ($nextArr as $id){
                                    $nextStr.=  $this->mean[$key][$id].' ';
                                }
                            }
                            $content[$meanInfo] = $preStr.' -> '.$nextStr;
                        }else{
                            if(!$this->pre[$key])
                            {
                                $pre_content='';
                            }
                            else
                            {
                                $pre_content=$this->mean[$key][ $this->pre[$key]];
                            }
                            $content[$meanInfo] =  $pre_content.' -> '. $this->mean[$key][$val];
                        }
                    }else{
                        if ($key == 'pic' || $key == 'pics'){
                            $content[$meanInfo] = "修改图片";
                        }else{
                            $content[$meanInfo] =  $this->pre[$key].' -> '. $this->next[$key];
                        }
                    }
                }
            }
        }

        $data = [
            'module' =>  $this->module->pid,
            'operate' =>  $this->module->id,
            'type' =>  $this->module->type,
            'main' =>  isset($this->next['name']) ? $this->next['name'] : $this->pre['name'],
            'main_id' => isset($this->pre['id']) ? $this->pre['id'] : 0,
            'content' => json_encode($content,JSON_UNESCAPED_UNICODE),
            'user_id' => auth('admin')->user()->id,
            'ip'    => get_proxy_ip(),
            'model' => $this->module->model,
        ];

        return $data;
    }
}
